from django.db import models

from user.models import User


# Create your models here.
class ArticleColumn(models.Model):
    """文章分栏模型类"""
    column = models.CharField(max_length=128, verbose_name='文章分栏')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='所属用户')
    created = models.DateTimeField(auto_now_add=True, verbose_name='创建时间')

    class Meta:
        db_table = 'tb_article_column'
        verbose_name = '文章分栏'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.column}'


class ArticleTag(models.Model):
    """文章标签模型类"""
    tag = models.CharField(max_length=64, verbose_name='文章标签')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='所属用户')

    class Meta:
        db_table = 'tb_article_tag'
        verbose_name = '文章标签'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.tag}'


class Article(models.Model):
    """文章模型类"""
    STATUS_CHOICES = (
        (0, '草稿箱'),
        (1, '已发布'),
        (2, '待审核'),
        (3, '审核未通过'),
        (4, '回收站'),
    )
    TITLE_CHOICES = (
        (0, '原创'),
        (1, '转载'),
        (2, '翻译'),
    )
    SCOPE_CHOICES = (
        (0, '全部可见'),
        (1, '仅我可见'),
        (2, '粉丝可见'),
        (3, 'VIP可见'),
    )
    title = models.CharField(max_length=100, verbose_name='文章标题')
    author = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='作者')
    abstract = models.TextField(max_length=256, null=True, blank=True, verbose_name='文章摘要')
    publish_date = models.DateTimeField(auto_now_add=True, verbose_name='发布日期')
    update_date = models.DateTimeField(auto_now=True, verbose_name='上次修改')
    status = models.SmallIntegerField(choices=STATUS_CHOICES, default=0, verbose_name='发布状态')
    type = models.SmallIntegerField(choices=SCOPE_CHOICES, default=0, verbose_name='文章类型')
    scope = models.SmallIntegerField(choices=SCOPE_CHOICES, default=0, verbose_name='可见范围')
    reading_volume = models.IntegerField(default=0, verbose_name='阅读量')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')
    column = models.ManyToManyField(ArticleColumn, verbose_name='文章分栏', blank=True, db_table='tb_articles_columns')
    tag = models.ManyToManyField(ArticleTag, verbose_name='文章标签', blank=True, db_table='tb_articles_tags')

    class Meta:
        db_table = 'tb_article'
        verbose_name = '博客文章'
        verbose_name_plural = verbose_name

    def __str__(self):
        return self.title


class ArticleContent(models.Model):
    """文章内容模型类"""
    content = models.TextField(verbose_name='文章内容')
    article = models.OneToOneField(Article, on_delete=models.CASCADE, verbose_name='所属文章')

    class Meta:
        db_table = 'tb_article_content'
        verbose_name = '文章内容'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.article}'


class ArticleComment(models.Model):
    """文章评论模型类"""
    comment = models.CharField(max_length=128, verbose_name='评论')
    date = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')
    article = models.ForeignKey(Article, on_delete=models.CASCADE, verbose_name='所属文章')
    user = models.ForeignKey(User, on_delete=models.CASCADE, verbose_name='所属用户')
    parent = models.ForeignKey('self', on_delete=models.CASCADE, null=True, blank=True, verbose_name='父级评论')
    is_delete = models.BooleanField(default=False, verbose_name='逻辑删除')

    class Meta:
        db_table = 'tb_article_comment'
        verbose_name = '文章评论'
        verbose_name_plural = verbose_name

    def __str__(self):
        return f'{self.user}<{self.article}>:{self.comment}'
